
function createAccount() {
 var a = new AccountEntity();
 
 a.id = "test";
 a.passwd = "password";
 a.jsonRoles = "[ 'administrator', 'b2b' ]";
 a.tenant = "DEVELOP";
  
 EntityManager.createAccount( a );
 
 return a; 
}

function registerUser( login, deviceId ) {

  var a = EntityManager.findAccountByLogin( login) ;
  
  var u = new UserEntity();
  
  u.deviceId = deviceId;
 
  a.setUser( u );
  
  EntityManager.registerUser( u, a );
  

  return u;
}

function updateUserPreferences( login, prefs ) {
  
    var a = EntityManager.findAccountByLogin( login) ;
    
    var u = a.getUser();
    
    u.setPreferences( toJSON(prefs) );
  
    EntityManager.storeUser( u );
  
    return u;

}

//
// usage: 
// newTransaction( { attrs:{}, agent:'', type:'test', user:'' } )
//
function newTransaction( tt ) {
	  
    var t = new TransactionEntity();
    
	t.setAttributes( toJSON( tt.attrs ) );
	t.userAgent = tt.agent;
	t.setDate( new Date() );
	t.userId = tt.user;
	t.type = tt.type;

	EntityManager.createTransaction( t );
  
    return t ;

}

//
//usage: 
//newTransaction( { attrs:{}, agent:'', type:'test', user:'' } )
//
function getTransactions() {

	var records = new JSONArray();
	
	
    EntityManager.findAllTransactions( function(result) {
    	
    	for( var i = 0; i < result.size() ; ++i ) {
    		
    		records.put( JSONObject.wrap(result.get(i)) );
    	}
    	
    });
    
    var result = new JSONObject();
    
    result.put( "records", records );
    

    return result.toString() ;

}
